package com.pucong.gitee.database.backup.business.sys.controller;

import com.pucong.gitee.database.backup.business.sys.service.SysFileService;
import com.pucong.gitee.database.backup.common.entity.sys.SysFile;
import com.pucong.gitee.database.backup.utils.Res;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author pucong
 * @company gitee.com/pucong
 * @create 2020-12-02
 */
@RestController
@RequestMapping("/sys/sysFile")
@Api(value = "附件操作-API ",tags = "03.附件操作")
public class SysFileController {

    @Autowired
    private SysFileService sysFileService;

    @GetMapping("/getSysFileById/{id}")
    @ApiOperation(value = "01根据主键查询")
    public Res<SysFile> getSysFileById(@ApiParam("主键")@PathVariable("id") String id){
        SysFile sysFile = sysFileService.getSysFileById(id);
        return Res.ok(sysFile);
    }

    @GetMapping("/getSysFileByBusinessId/{businessId}")
    @ApiOperation(value = "02根据业务id查询")
    public Res<List<SysFile>> getSysFileByBusinessId(@ApiParam("业务id")@PathVariable("businessId") String businessId){
        List<SysFile> sysFiles = sysFileService.getSysFileByBusinessId(businessId);
        return Res.ok(sysFiles);
    }

    @PostMapping("/uploadFile")
    @ApiOperation(value = "03文件上传")
    public Res<SysFile> uploadFile(@RequestParam("file") MultipartFile file) {
        SysFile sysFile = sysFileService.uploadFile(file);
        return Res.ok(sysFile);
    }

    @DeleteMapping("/deleteFileById/{id}")
    @ApiOperation(value = "04删除上传的文件")
    public Res<Integer> deleteFileById(@ApiParam("文件id")@PathVariable("id") String id) {
        int count = sysFileService.deleteFileById(id);
        return Res.ok(count);
    }

    @DeleteMapping("/deleteFileByBusinessId/{businessId}")
    @ApiOperation(value = "05根据业务id删除上传的文件")
    public Res<Integer> deleteFileByBusinessId(@ApiParam("业务表id")@PathVariable("businessId") String businessId) {
        int count = sysFileService.deleteFileByBusinessId(businessId);
        return Res.ok(count);
    }

    @GetMapping("/downLoadFileById/{uuid}")
    @ApiOperation(value = "06下载文件")
    public void downLoadFileById(@ApiParam("附件id")@PathVariable("uuid") String uuid, HttpServletRequest request, HttpServletResponse response) {
        sysFileService.downLoadFileById(uuid,request,response);
    }

    @GetMapping("/lookFileById/{uuid}")
    @ApiOperation(value = "07预览pdf、图片文件")
    public void lookFileById(@ApiParam("业务表id")@PathVariable("uuid") String uuid, HttpServletRequest request, HttpServletResponse response) {
        sysFileService.lookFileById(uuid,request,response);
    }
}
